package com.cnb.telepathytalk.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cnb.telepathytalk.R;
import com.cnb.telepathytalk.dto.RealTimeSearchWordDTO;

public class RealTimeSearchWordGridAdapter extends BaseAdapter {
	private ArrayList<RealTimeSearchWordDTO> list;
	private LayoutInflater inflater;
	private int layoutRescId;

	public RealTimeSearchWordGridAdapter(Context context) {
		super();
		this.list = new ArrayList<RealTimeSearchWordDTO>();
		this.inflater = LayoutInflater.from(context);
		this.layoutRescId = R.layout.grid_item_realtime_search_word;
	}

	public ArrayList<RealTimeSearchWordDTO> getList() {
		return list;
	}

	@Override
	public int getCount() {
		return 9;
	}

	@Override
	public RealTimeSearchWordDTO getItem(int position) {
		return list.get(position); 
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(layoutRescId, null);
			holder = new Holder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		try {
			RealTimeSearchWordDTO item = list.get(position);
			holder.getKeyword().setText(item.getKeyword());
		} catch (Exception e) {
		}

		holder.getRank().setText("" + (position + 1));

		return convertView;
	}

	private class Holder {
		private View baseView;
		private TextView rank;
		private TextView keyword;

		public Holder(View view) {
			super();
			baseView = view;
		}

		public TextView getRank() {
			if (rank == null) {
				rank = (TextView) baseView.findViewById(R.id.textView1);
			}
			return rank;
		}

		public TextView getKeyword() {
			if (keyword == null) {
				keyword = (TextView) baseView.findViewById(R.id.textView2);
			}
			return keyword;
		}
	}
}